package com.qububu.db.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.ShoppingCartDao;
import com.qububu.emporium.shoppingCart.dto.CartSaveGoodsMessageDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Repository
public class ShoppingCartDaoImpl implements ShoppingCartDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/**
	 * 根据用户ID查找该用户存放在购物车中的信息
	 * */
	public List<CartSaveGoodsMessageDto> searchCartSaveGoodsMessages(int cartId) {
		
		List<CartSaveGoodsMessageDto> cartSaveGoodsMessageList = null;
		// 应该先删除数据库中的信息, 另一种思路, 这里不删除, 后面保存的时候先判断(根据userId和goodsId)如果有这个数据则修改如果没有则插入
//		String sql = "select * from cart where cartId = ?";
		String sql = "delete from cart where cartId = ?";
		
		List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql, cartId);
		
		cartSaveGoodsMessageList = new ArrayList<CartSaveGoodsMessageDto>();
		CartSaveGoodsMessageDto cartSaveGoodsMessageDto = null;
		for (Map<String, Object> elem : resultList) {
			int goodsId = ((Long) elem.get("goods_id")).intValue();
			cartSaveGoodsMessageDto = new CartSaveGoodsMessageDto(goodsId);
			cartSaveGoodsMessageDto.setGoodsName((String) elem.get("goods_name") );
			cartSaveGoodsMessageDto.setGoodsPicUrl((String) elem.get("goods_pic_url") );
			cartSaveGoodsMessageDto.setGoodsPrice((String) elem.get("goods_price") );
			cartSaveGoodsMessageDto.setGoodsQuantity(((Long) elem.get("goods_quantity")).intValue() );
			cartSaveGoodsMessageDto.setGoodsUseNum(((Long) elem.get("goods_use_num")).intValue() );
			cartSaveGoodsMessageDto.setAddGoodsDttm((Long) elem.get("add_goods_dttm") );
			
			cartSaveGoodsMessageList.add(cartSaveGoodsMessageDto);
		}
		
		return cartSaveGoodsMessageList;
	}

	public void saveGoodsMessageList(final int cartId,
			final List<CartSaveGoodsMessageDto> cartGoodsMessageList) {
		// 批量修改
		String sql = "insert into cart(user_id, goods_id, goods_name, goods_price, goods_pic_url, goods_use_num, goods_quantity, add_goods_dttm) values(?, ?, ?, ?, ?, ?, ?, ?)";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			
			public void setValues(PreparedStatement ps, int listValueNo) throws SQLException {
				CartSaveGoodsMessageDto cartSaveGoodsMessage = cartGoodsMessageList.get(listValueNo);
				int goodsId = Integer.parseInt(SystemUtil.decrypt(cartSaveGoodsMessage.getGoodsId() ) );
				int parameterIndex = 0;
				ps.setInt(++ parameterIndex, cartId);
				ps.setInt(++ parameterIndex, goodsId);
				ps.setString(++ parameterIndex, cartSaveGoodsMessage.getGoodsName());
				ps.setString(++ parameterIndex, cartSaveGoodsMessage.getGoodsPrice());
				ps.setString(++ parameterIndex, cartSaveGoodsMessage.getGoodsPicUrl());
				ps.setInt(++ parameterIndex, cartSaveGoodsMessage.getGoodsUseNum());
				ps.setInt(++ parameterIndex, cartSaveGoodsMessage.getGoodsQuantity());
				ps.setLong(++ parameterIndex, cartSaveGoodsMessage.getAddGoodsDttm());
			}
			
			public int getBatchSize() {
				return cartGoodsMessageList.size();
			}
			
		});
	}

	public CartSaveGoodsMessageDto getCartSaveGoodsMessageByGoodsId(
			String goodsId) {
		
		String sql = "SELECT * FROM goods_info WHERE goods_id=?";
		List<Map<String, Object>> goodsMessageList = jdbcTemplate.queryForList(sql, ParamConverter.convertToInteger(SystemUtil.decrypt(goodsId)));
		CartSaveGoodsMessageDto goods = null;
		if (goodsMessageList != null && goodsMessageList.size() > 0) {
			Map<String, Object> goodsMap = goodsMessageList.get(0);
			goods = new CartSaveGoodsMessageDto(goodsId);
			goods.setGoodsName(String.valueOf(goodsMap.get("goods_name")));
			goods.setGoodsPicUrl(String.valueOf(goodsMap.get("goods_pic_url")));
			goods.setGoodsPrice(String.valueOf(goodsMap.get("goods_price")));
			goods.setGoodsUseNum(Integer.parseInt(String.valueOf(goodsMap.get("goods_use_num"))));
		}
		
		return goods;
	}

}
